package com.aliyun.heiyu.powermaster.server.database;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnectUtil {
    private static Logger logger = LoggerFactory.getLogger(DatabaseConnectUtil.class);

    private static String driver = "com.mysql.jdbc.Driver";
    private static String url1 = "jdbc:mysql://";
    private static String url2 = "?useUnicode=true&characterEncoding=UTF-8";
    public static Connection connect(String host, int port, String username, String p, String database){
        try {
            Class.forName(driver);
            String url = getUrl(host, port, database);
            Connection conn = DriverManager.getConnection(
                url, username, p);
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getUrl(String host, int port, String database){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(url1).append(host).append(":").append(port).append("/").append(database).append(url2);
        return stringBuffer.toString();
    }

}
